package com.yuke.cloud.service.omc.controller.frontend;

import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.omc.exceptions.OmcBizException;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoExDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.omc.dto.CartInfoDto;
import com.yuke.cloud.service.omc.dto.CartSpecDto;
import com.yuke.cloud.service.omc.entity.OdsShopCart;
import com.yuke.cloud.service.omc.service.OdsShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yuke-mall </br> </p>
 * <p>com.yuke.cloud.service.omc.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2019/11/23 17:33
 */
@Api(value = "WEB - OdsStorageOrderController", tags = "处理OdsShopCart相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/Storage")
public class OdsStorageOrderController extends BaseController {
    @Resource
    private OdsShopCartService odsShopCartService;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;

    @GetMapping("/foruser")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<CartInfoDto> list2(@ApiParam(name = "userId", value = "记录") @RequestParam(defaultValue = "0") Long userId,
                                      @ApiParam(name = "shopId", value = "店铺", defaultValue = "0") @RequestParam(defaultValue = "0") Long shopId,
                                      @ApiParam(name = "cartType", value = "购物车类型", defaultValue = "1") @RequestParam(defaultValue = "1") Byte cartType) {


        //cmcFeignApi.loadSecKillAndSpecs();
        if (PublicUtil.isEmpty(userId)) {
            throw new OmcBizException(ErrorCodeEnum.GL99990100);
        }

        List<Long> storageIds = new ArrayList<>();
        List<Long> specIds = new ArrayList<>();
        CartInfoDto cartInfoDto = new CartInfoDto();
        cartInfoDto.setUserId(userId);
        List<CartSpecDto> cartSpecDtos = new ArrayList<>();
        cartInfoDto.setCartSpecDtos(cartSpecDtos);

        // mod by wg 20190410 修改成可以根据店铺及用户返回购物车
        Example example = new Example(OdsShopCart.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("create_time desc");
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("cartType", cartType);
        List<OdsShopCart> list = odsShopCartService.selectByExample(example);
        if (list.size() == 0) {
            return WrapMapper.ok(cartInfoDto);
        }


        for (OdsShopCart odsShopCart : list) {
            if (odsShopCart.getStorageId() != null && odsShopCart.getStorageId() > 0){
                storageIds.add(odsShopCart.getStorageId());
            }

            CartSpecDto cartSpecDto = new CartSpecDto();
            cartSpecDto.setSpecId(odsShopCart.getSpecId());
            cartSpecDto.setNum(odsShopCart.getNum());
            cartSpecDto.setShopId(odsShopCart.getShopId());
            cartSpecDtos.add(cartSpecDto);
            specIds.add(odsShopCart.getSpecId());
        }

        logger.info("foruser:{}", cartSpecDtos);
        List<ProdSpecInfoExDto> prodSpecInfoExDtoList = new ArrayList<>();
        if (storageIds != null && storageIds.size() > 0 && storageIds.size() == list.size()){
            prodSpecInfoExDtoList  = wmcStorageFeignApi.getStorageSpecByStorageIds(storageIds,cartType);
        }else{
            prodSpecInfoExDtoList =wmcStorageFeignApi.getStorageSpecBySpecIds(shopId,specIds,cartType);
        }

        System.out.println("prodSpecInfoExDtoList" + prodSpecInfoExDtoList);
        if (prodSpecInfoExDtoList != null && prodSpecInfoExDtoList.size() > 0) {
            for (ProdSpecInfoExDto prodSpecInfoExDto : prodSpecInfoExDtoList) {
                for (CartSpecDto cartSpecDto : cartSpecDtos) {
                    if (cartSpecDto.getSpecId().equals(prodSpecInfoExDto.getSpecId())) {
                        cartInfoDto.setCartSpecDtos(cartSpecDtos);
                        cartSpecDto.setProdSpecInfoExDto(prodSpecInfoExDto);
                        //break;
                    }
                }
            }
        }

        List<CartSpecDto> cartSpeclist = new ArrayList<>();
        cartInfoDto.getCartSpecDtos().stream().collect(Collectors.groupingBy(CartSpecDto::getSpecId)).forEach((k, kList) ->
        {
            CartSpecDto aaa = new CartSpecDto();
            IntSummaryStatistics nmap = kList.stream().collect(Collectors.summarizingInt(e -> Integer.valueOf(e.getNum().toString())));
            aaa.setSpecId(kList.get(0).getSpecId());
            aaa.setShopId(kList.get(0).getShopId());
            aaa.setNum(Integer.valueOf(String.valueOf(nmap.getSum())));
            aaa.setSpecFrom(kList.get(0).getSpecFrom());
            aaa.setProdSpecInfoExDto(kList.get(0).getProdSpecInfoExDto());
            cartSpeclist.add(aaa);
        });

        cartInfoDto.setCartSpecDtos(cartSpeclist);

        return WrapMapper.ok(cartInfoDto);
    }

    @GetMapping("/foruser/{userId}")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<CartInfoDto> list3(@ApiParam(name = "userId", value = "记录") @PathVariable Long userId,
                                      @ApiParam(name = "shopId", value = "店铺", defaultValue = "0") @RequestParam(defaultValue = "0") Long shopId,
                                      @ApiParam(name = "cartType", value = "购物车类型", defaultValue = "1") @RequestParam(defaultValue = "1") Byte cartType) {

        return this.list2(userId,shopId,cartType);
    }


}
